var path = require('path');
var fs = require('fs');
var log = console.log;
var config = require("./config");
var Oauth = require("./utils/utils");
var multiparty = require('multiparty');
var Index = require("./app/controllers/index");
var Admin = require("./app/controllers/admin");
var scssPath = './public/activity/';
var articlePath = './database/articles.json';

module.exports = function (app) {

  // 文件上传
  app.post('/upload', function (req, res, next) {
    var form = new multiparty.Form({ uploadDir: 'public/files/' });

    form.parse(req, function (err, fields, files) {
      var distPath = '/' + files.upload[0].path;

      res.send({
        'status': 'ok',
        'path': distPath
      });
    });
  });

  // 前端首页
  app.get(['/', '/index'], Oauth.login, Index.index);
  // ui 首页
  app.get(['/ui','/ui?msid='], Oauth.login, Index.index);

  app.get('/preview', Oauth.login, Index.preview);
  // 保存普通模块
  app.post('/save_ms_normal', Oauth.login, Index.ms_normal);
  // 保存抢购模块
  app.post('/save_ms_flash', Oauth.login, Index.ms_flash);

  // 后台管理:
  app.get('/login', function (req, res) {
    var user = req.session.user;
    if (user) {
      res.redirect("/");
    } else {
      res.render('login', {config: config, title:'请选择你的身份'});
    }
  });

  app.post('/postlogin', Admin.postLogin);
};